package com.szhuang.mobilesafe.adapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Vibrator;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.szhuang.mobilesafe.R;
import com.szhuang.mobilesafe.activity.LostFindActivity;
import com.szhuang.mobilesafe.activity.MainActivity;
import com.szhuang.mobilesafe.activity.SettingActivity;
import com.szhuang.mobilesafe.tools.Md5Utils;

import java.util.ArrayList;

public class MyPagerAdapter extends PagerAdapter {

	Vibrator vibrator;
	ArrayList<MainActivity.ImageInfo> data;
	Activity activity;
	LayoutParams params;

    private AlertDialog dialog_setup_pwd;
    private AlertDialog dialog_input_pwd;
    private EditText setup_pwd;
    private EditText confirm_pwd;
    private EditText pwd;
    private Button ok;
    private Button cancel;
    private Button btn_ok;
    private Button btn_cancel;

    private SharedPreferences sp;
	public MyPagerAdapter(Activity activity, ArrayList<MainActivity.ImageInfo> data) {
		this.activity = activity;
		this.data = data;
		vibrator = (Vibrator) activity.getSystemService(Context.VIBRATOR_SERVICE);
        sp=sp=activity.getSharedPreferences("config",Context.MODE_PRIVATE);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return 2;
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		// TODO Auto-generated method stub
		return arg0 == arg1;
	}

	@Override
	public Object instantiateItem(ViewGroup container, final int index) {
		Log.v("test", index + "index");

		View view = activity.getLayoutInflater().inflate(R.layout.grid, null);
		GridView gridView = (GridView) view.findViewById(R.id.gridView1);
		gridView.setNumColumns(2);
        gridView.setGravity(Gravity.CENTER);
        gridView.setVerticalSpacing(25);
        gridView.setHorizontalSpacing(25);
		gridView.setAdapter(new BaseAdapter() {

			@Override
			public int getCount() {
				return 8;
			}

			@Override
			public Object getItem(int position) {
				return position;
			}

			@Override
			public long getItemId(int position) {
				return 8*index+position;
			}

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				View item = LayoutInflater.from(activity).inflate(R.layout.grid_item, null);
				ImageView iv = (ImageView) item.findViewById(R.id.imageView1);
				iv.setImageResource((data.get(index * 8 + position)).imageId);
				TextView tv = (TextView) item.findViewById(R.id.msg);
				tv.setText((data.get(index * 8 + position)).imageMsg);
				return item;
			}
		});

		gridView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				View view = arg1;
				arg1.setVisibility(View.INVISIBLE);

				params = new WindowManager.LayoutParams();
				activity.getWindowManager().addView(view, params);
				vibrator.vibrate(2500);
				return true;
			}
		});

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(activity,data.get((int)id).imageMsg,Toast.LENGTH_SHORT).show();

                switch ((int)id){
                    case 0:
                        enterAntiTherf();

                        break;
                    case 1:

                        break;
                    case 2:

                        break;
                    case 3:

                        break;
                    case 4:

                        break;
                    case 5:

                        break;
                    case 6:

                        break;
                    case 7:

                        break;
                    case 8:
                        Intent intent=new Intent(activity,SettingActivity.class);
                        activity.startActivity(intent);
                        break;
                }
            }
        });

		((ViewPager) container).addView(view);

		return view;
	}

    public void enterAntiTherf(){
        if(isSetUpPwd()){

            showInputPwdDialog();
        }else{
            showSetUpPwdDialog();

        }
    }

    private void showInputPwdDialog() {

        AlertDialog.Builder builder=new AlertDialog.Builder(activity);
        View view=View.inflate(activity,R.layout.activity_dialog_input_password,null);
        builder.setView(view);
        dialog_input_pwd=builder.create();
        pwd= (EditText) view.findViewById(R.id.pwd);
        ok=(Button) view.findViewById(R.id.ok);
        cancel=(Button) view.findViewById(R.id.cancel);

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog_input_pwd.dismiss();
            }
        });
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String password=pwd.getText().toString().trim();
                if(TextUtils.isEmpty(password)){
                    Toast.makeText(activity,"密码不为空！",Toast.LENGTH_LONG).show();
                    return;
                }
                String savePassword=sp.getString("password","");
                if(Md5Utils.encode(password).equals(savePassword)){

                    dialog_input_pwd.dismiss();
                    Intent intent=new Intent(activity, LostFindActivity.class);
                    activity.startActivity(intent);

                }else{
                    pwd.setText("");
                    Toast.makeText(activity,"密码错误！",Toast.LENGTH_SHORT).show();
                }

            }
        });
        dialog_input_pwd.show();
    }

    private void showSetUpPwdDialog() {
        AlertDialog.Builder builder=new AlertDialog.Builder(activity);
        View view=View.inflate(activity,R.layout.activity_dialog_setup_password,null);
        builder.setView(view);
        dialog_setup_pwd=builder.create();
        setup_pwd= (EditText) view.findViewById(R.id.setup_pwd);
        confirm_pwd=(EditText) view.findViewById(R.id.confirm_pwd);
        btn_ok=(Button) view.findViewById(R.id.btn_ok);
        btn_cancel=(Button) view.findViewById(R.id.btn_cancel);

        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog_setup_pwd.dismiss();
            }
        });
        btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String password=setup_pwd.getText().toString().trim();
                String password_confirm=confirm_pwd.getText().toString().trim();
                if(TextUtils.isEmpty(password)){
                    Toast.makeText(activity,"密码不为空！",Toast.LENGTH_LONG).show();
                    return;
                }
                if(password.equals(password_confirm)){

                    SharedPreferences.Editor editor=sp.edit();
                    editor.putString("password", Md5Utils.encode(password));
                    editor.commit();
                    dialog_setup_pwd.dismiss();
                }else{

                    Toast.makeText(activity,"密码不一致！",Toast.LENGTH_SHORT).show();
                }

            }
        });
        dialog_setup_pwd.show();

    }

    private boolean isSetUpPwd(){

        return !TextUtils.isEmpty(sp.getString("password",null));
    }
}
